<?php
namespace console\controllers;

use yii;
use yii\console\Controller;

class DuliController extends Controller
{
    //商户信息
    //key 为商户id
    //数据为商户信息
    public $business = [
        
        '118750' =>[
            'host'=>'116.62.217.223',
        ],
    ];
    
    /**
     * 余额检测
     */
    public function actionBalance()
    {
        //主库连接
        $master = new yii\db\Connection([
            'dsn' => 'mysql:host=rr-bp10py0xbj3k4wcshuo.mysql.rds.aliyuncs.com;dbname=xindaishuju',
            'username' => 'ecbumizde',
            'password' => 'OG8l5a2xRAVWcQcG',
            'charset' => 'utf8',
            'tablePrefix' => 'busy_'
        ]);

        foreach ($this->business as $key => $item) {
            
            $host = $item['host'];
            $dbname = isset($item['dbname']) ? $item['dbname'] : 'xddb';
            $username = isset($item['username']) ? $item['username'] : 'dladmin';
            $password = isset($item['password']) ? $item['password'] : 'root@123$';

            //查询独立商户的余额
            $db = new yii\db\Connection([
                'dsn' => "mysql:host=$host;dbname=$dbname",
                'username' => "$username",
                'password' => "$password",
                'charset' => 'utf8',
                'tablePrefix' => 'busy_'
            ]);
            
            $sql = "select id,user_balance from busy_admin_user where id = :id";
            
            $data_duli = $db->createCommand($sql)->bindValue(':id', $key)->queryOne();
            
            //查询主库数据的余额
            $data_master = $master->createCommand($sql)->bindValue(':id', $key)->queryOne();

            echo $data_duli['id'].'---'.$data_duli['user_balance'].'----'.$data_master['user_balance']."\n";
            
        }
    }
    
    public function actionBalanceDetail($admin_id = 0)
    {
        if (intval($admin_id) == 0) {
            exit("admin empty \n");
        }
        
        if (!array_key_exists($admin_id, $this->business)) {
            exit("admin config not setting \n");
        }
        
        $business =  $this->business[$admin_id];
        
        $host = $business['host'];
        $dbname = isset($business['dbname']) ? $business['dbname'] : 'xddb';
        $username = isset($business['username']) ? $business['username'] : 'dladmin';
        $password = isset($business['password']) ? $business['password'] : 'root@123$';
        
        //主库连接
        $master = new yii\db\Connection([
            'dsn' => 'mysql:host=rr-bp10py0xbj3k4wcshuo.mysql.rds.aliyuncs.com;dbname=xindaishuju',
            'username' => 'ecbumizde',
            'password' => 'OG8l5a2xRAVWcQcG',
            'charset' => 'utf8',
            'tablePrefix' => 'busy_'
        ]);
        
        //查询独立商户的余额
        $db = new yii\db\Connection([
            'dsn' => "mysql:host=$host;dbname=$dbname",
            'username' => "$username",
            'password' => "$password",
            'charset' => 'utf8',
            'tablePrefix' => 'busy_'
        ]);
        
        $startID = '';

        $sql_total = "select count(*) as count from busy_expense where id >= :id and userid =:admin_id";
        
        $count = $db->createCommand($sql_total)
        ->bindValue(':id', $startID)
        ->bindValue(':admin_id', $admin_id)
        ->queryOne();
        
        if (!$count['count']) {
            die("无数据\n");
        }
        
        for ($i = 0; $i < $count['count']; $i++) {
            
            $duli_expense = (new \yii\db\Query())
            ->from('busy_expense')
            ->where(['>=', 'id', $startID])
            ->orderBy('id asc')
            ->offset($i)
            ->limit(1)
            ->one();
            
            $master_expense = (new \yii\db\Query())
            ->from('busy_expense')
            ->where(['userid'=>$admin_id])
            ->andWhere(['>=', 'id', $startID])
            ->orderBy('id asc')
            ->offset($i)
            ->limit(1)
            ->one();
            
            if ($duli_expense['belongid'] != $master_expense['belongid'] 
                || $duli_expense['message'] != $master_expense['message']
                || $duli_expense['user_balance'] != $master_expense['user_balance']
                ) {
                    
                echo json_encode($master_expense);
                echo "\n";
                exit;
            }
               
        }
    }
    
}